<?php
/**
 * 
 */
class Refund_model extends BS_Model
{
	/**
	 * 插入退款记录
	 * @param  array $refund 退款记录数组
	 * @return int         退款记录id
	 */
	public function insertRefund($refund)
	{
        $query = $this->db->insert('refund', $refund);
        if($query === false)
        {
        	return false;
        }
        return $this->db->insert_id();
	}

	/**
	 * 根据时间范围获取退款记录
	 * @param  [int] $starttime [起始搜索时间]
	 * @param  [int] $endtime   [终止搜索时间]
	 * @return [array(array())]            [二维数组]
	 */
	public function getRefunds($starttime = null, $endtime = null)
	{
		$this->db->select('*');
        $this->db->from('refund');
        isset($starttime) && $this->db->where('dateline >', $starttime);
        isset($endtime) && $this->db->where('dateline <', $endtime);
        $query = $this->db->get();
        if ($query->num_rows() >= 0) {
            return $query->result_array();
        }
        return array();
	}

	/**
	 * 事务处理退款数据
	 * @param  array  $refunds [description]
	 * @return bool   事务运行成功:true 失败:false
	 */
	public function dealRefunds($refunds = array())
	{
		$this->load->model('Order_model', 'order_model');
        $this->load->model('Orderdetail_model');
        $time_now = time();
        $this->db->trans_start();
		//循环导入数据
		foreach ($refunds as $key => $value) {
			//修改订单信息
			$order = $this->order_model->getOrderById($value['order_id']);
			$orderSet['return_money'] = $order['return_money'] + $value['refund_price'];
			$orderSet['update_time'] = $time_now;
			//全部退款完成，修改订单状态为5:已退押金
			if($orderSet['return_money'] >= $order['price'])
			{
				$orderSet['status'] = 5;
			}
			$this->order_model->updateOrderByOrderid($value['order_id'], $orderSet);
			//修改订单详情信息
			$order_detail = $this->Orderdetail_model->getOrderDetailByOrderDetailId($value['order_detail_id']);
			$orderDetailSet['return_money'] = $order_detail['return_money'] + $value['refund_price'];
			$orderDetailSet['last_return_time'] = $value['last_return_time'];
			$orderDetailSet['update_time'] = $time_now;
			//全部退款完成，修改订单详情状态为5:已退押金
			if($orderDetailSet['return_money'] >= $order_detail['price'])
			{
				$orderDetailSet['status'] = 5;
			}
			$this->Orderdetail_model->updateOrderDetailByorderDetailId($value['order_detail_id'], $orderDetailSet);
			unset($value['last_return_time']);
			//插入退款表
			$this->insertRefund($value);
		}
		$this->db->trans_complete();
		 // echo $this->db->last_query();die;
		return $this->db->trans_status();
	}

	/**
	 * 根据订单详情号获取所有退款时间
	 * @param  [type] $order_detail_id [description]
	 * @return array                  [description]
	 */
	public function getRefundTimeByOrderDetailId($order_detail_id)
	{
		$query = $this->db->select('refund_time')
						  ->from('refund')
						  ->where('order_detail_id', $order_detail_id)
						  ->get();
		if($query->num_rows() >= 0)
		{
			return $query->result_array();
		}
		return array();
	}

	/**
	 * 根据订单详情号获取所有退款流水
	 * @param  [type] $order_detail_id [description]
	 * @return array                  [description]
	 */
	public function getRefundListeByOrderDetailId($order_detail_id)
	{
		$query = $this->db->select('*')
						  ->from('refund')
						  ->where('order_detail_id', $order_detail_id)
						  ->get();
		if($query->num_rows() >= 0)
		{
			return $query->result_array();
		}
		return array();
	}


}